using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Collections;

namespace Memio
{
    public partial class Scores : Form
    {
        private class si
        {
            public String login;
            public Double results;
        }

        private class ListObject : IComparer
        {
            public enum Directions : int { ascending = 1, descending = (-1) };

            private int direction = (int)ListObject.Directions.ascending;

            public ListObject() { }

            public ListObject(Directions direction) { this.direction = (int)direction; }

            public int Compare(object x, object y)
            {
                si xs = (si)x;
                si ys = (si)y;
                return (xs.results > ys.results ? -1 : 1);
            }
        }


        public Scores()
        {
            InitializeComponent();
            listBox1.Items.Clear();
        }

        public void AppendUsers(ref XmlDocument users)
        {
            
            ArrayList al = new ArrayList();
            si obj;

            foreach (XmlNode n in users.SelectNodes("/users/login"))
            {
                obj = new si();
                obj.login = n.SelectSingleNode("name").InnerText;
                obj.results = Convert.ToDouble(String.IsNullOrEmpty(n.SelectSingleNode("results").InnerText) ? "0" : n.SelectSingleNode("results").InnerText);
                al.Add(obj);
            }
            al.Sort(new ListObject(ListObject.Directions.descending));

            for (int i = 0; i < al.Count; i++)
                listBox1.Items.Add( Convert.ToString(i+1) + ". " + ((si)al[i]).login +  " (" + Convert.ToString(((si)al[i]).results * 100) + " %)");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}